package com.ph.irp.base.verifyproject.action;

import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectCandiateService;
import com.ph.irp.dto.CandiateDTO;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.model.PBaseVerifyprojectCandiate;
import com.ph.util.pagination.PageUtil;
import com.ph.util.pagination.ShowPageList;

public class VerifyprojectCandiateAction extends ActionSupport implements
		ShowPageList {
	private PageDTO page;
	private String cpage;

	private PBaseVerifyprojectCandiateService candiateService;

	public void setCandiateService(
			PBaseVerifyprojectCandiateService candiateService) {
		this.candiateService = candiateService;
	}

	private PBaseVerifyprojectCandiate candiate;

	private CandiateDTO can;

	private List<CandiateDTO> cans;

	private List<CandiateDTO> tempcans;

	private Object[] rowlist = null;

	public PBaseVerifyprojectCandiate getCandiate() {
		return candiate;
	}

	public void setCandiate(PBaseVerifyprojectCandiate candiate) {
		this.candiate = candiate;
	}

	public CandiateDTO getCan() {
		return can;
	}

	public void setCan(CandiateDTO can) {
		this.can = can;
	}

	public List<CandiateDTO> getCans() {
		return cans;
	}

	public void setCans(List<CandiateDTO> cans) {
		this.cans = cans;
	}

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public String showCandiateList() throws Throwable {
		candiate = null;
		tempcans = candiateService.getCandiateList(candiate);
		page = PageUtil.generatePageDTO(tempcans,
				"showCandiatePageList.action", cpage);
		cans = page.getResList();
		return "success";
	}

	public String showPageList() throws Throwable {
		if (tempcans != null) {
			page = PageUtil.generatePageDTO(tempcans,
					"showCandiatePageList.action", cpage);
			cans = page.getResList();
			return "success";
		} else {
			return "input";
		}
	}

	public String getCandiateByCondition() throws Throwable {
		String name = candiate.getCandiateName().trim();
		String code = candiate.getCandiateCode().trim();
		candiate.setCandiateCode(code);
		candiate.setCandiateName(name);
		tempcans = candiateService.getCandiateList(candiate);
		page = PageUtil.generatePageDTO(tempcans,
				"showCandiatePageList.action", cpage);
		cans = page.getResList();
		candiate = null;
		return "success";
	}

	// List页面跳转到update修改页面
	public String showEditCandiate() throws Throwable {
		Integer candiateId = Integer.valueOf((String) rowlist[0]);
		can = candiateService.getCandiateById(candiateId);
		return "success";
	}

	public String editCandiate() throws Throwable {
		candiateService.updateCandiate(can.getCandiate());
		return "success";
	}

	public String addCandiate() throws Throwable {
		candiateService.addCandiate(can.getCandiate());
		can = null;
		return "success";
	}

	public String deleteCandiate() throws Throwable {
		for (int i = 0; i < rowlist.length; i++) {
			candiateService
					.deleteCandiate(Integer.valueOf((String) rowlist[i]));
		}
		return "success";
	}

	public String selectCandiate() throws Throwable {
		if(candiate != null){
			String name = candiate.getCandiateName().trim();
			String code = candiate.getCandiateCode().trim();
			candiate.setCandiateCode(code);
			candiate.setCandiateName(name);
		tempcans = candiateService.getCandiateList(candiate);}
		else{
			tempcans = candiateService.getCandiateList(new PBaseVerifyprojectCandiate());
		}
		page = PageUtil.generatePageDTO(tempcans,
				"showCandiatePageList.action", cpage);
		cans = page.getResList();
		candiate = null;
		return "success";
	}

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}
}
